package com.daluzy.chmanager.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Manager;
import com.daluzy.chmanager.bean.School;
import com.daluzy.chmanager.util.TelephoneUtil;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class AddSchoolActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_school_name)
    EditText etSchoolName;
    @BindView(R.id.et_school_phone)
    EditText etSchoolPhone;
    @BindView(R.id.et_school_address)
    EditText etSchoolAddress;
    @BindView(R.id.et_school_details)
    EditText etSchoolDetails;
    @BindView(R.id.tv_add_commit)
    TextView tvAddCommit;
    private String schoolName;
    private String schoolAddress;
    private String schoolTel;
    private String schoolDetails;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_school_daluzy);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this);
    }
    @OnClick({R.id.iv_back,R.id.tv_add_commit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_commit:
                check();
                tvAddCommit.setEnabled(false);
                break;
        }
    }

    private void check() {
        schoolName = etSchoolName.getText().toString().trim();
        schoolTel = etSchoolPhone.getText().toString().trim();
        schoolAddress = etSchoolAddress.getText().toString().trim();
        schoolDetails = etSchoolDetails.getText().toString().trim();

        if (TextUtils.isEmpty(schoolName)) {
            Toast.makeText(AddSchoolActivity.this, "请输入学校名称", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (TextUtils.isEmpty(schoolTel)) {
            Toast.makeText(AddSchoolActivity.this, "请输入学校联系方式", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (!(TelephoneUtil.checkCellphone(schoolTel) || TelephoneUtil.checkTelephone(schoolTel))) {
            Toast.makeText(AddSchoolActivity.this, "请输入正确的联系方式", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (TextUtils.isEmpty(schoolAddress)) {
            Toast.makeText(AddSchoolActivity.this, "请输入学校地址", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        if (TextUtils.isEmpty(schoolDetails)) {
            Toast.makeText(AddSchoolActivity.this, "请输入学校简介", Toast.LENGTH_SHORT).show();
            tvAddCommit.setEnabled(true);
            return;
        }
        addSchool();
    }

    private void addSchool(){
        if (loadingDialog != null)
            loadingDialog.show();
        School school = new School();
        school.setSchool_id(String.valueOf(System.currentTimeMillis()));
        school.setSchool_address(schoolAddress);
        school.setSchool_details(schoolDetails);
        school.setSchool_name(schoolName);
        school.setSchool_tel(schoolTel);
        school.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(AddSchoolActivity.this,"添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddSchoolActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null)
                    loadingDialog.close();
                tvAddCommit.setEnabled(true);
            }
        });
    }

}
